﻿
// This javascript sets the default value of a lookup field identified 
// by <<FIELD DISPLAY NAME>> to the value stored in the querysting variable
// identified by <<QUERYSTRING VARIABLE NAME>>

// Customize this javascript by replacing <<FIELD DISPLAY NAME>> and 
// <<QUERYSTRING VARIABLE NAME>> with appropriate values.
// Then just paste it into NewForm.aspx inside PlaceHolderMain

// Query String Format: dfn=<<displayName1>>&dft=<<type>>&dfv=<<value1>>&dfn=<<displayName2>>&dft=<<type>>&dfv=<<value2>>& ...
// Caution!!! Currently name parameter should go somewhere previously to value parameter

if (typeof(_spBodyOnLoadFunctionNames) != "undefined")
	_spBodyOnLoadFunctionNames.push("fillDefaultValues");

function fillDefaultValues() {
	var qs = location.search.substring(1, location.search.length);
	var args = qs.split("&");
	//var vals = new Object();
	var titles = new Object();
	var types = new Object();

	for (var i = 0; i < args.length; i++) {
		var nameVal = args[i].split("=");

		var name = nameVal[0];
		var val = nameVal[1];

		if (name.substring(0, 2) == "df") {
			val = val.split('+').join(' ');

			// It is default value
			val = decode(val, "utf8"); //unescape(val).split('+').join(' ')

			if (val.charAt(0) == '%')
				val = unescape(val);

			var lc = name.charAt(name.length - 1);

			if (lc == 'n') {
				// It is name parameter
				titles[name.substring(0, name.length - 1)] = val;
				//alert("dfn{"+name.substring(0, name.length - 1)+","+val+"}");
			} else if (lc == 't') {
				// It is type parameter
				types[name.substring(0, name.length - 1)] = val;
				//alert("dft{"+name.substring(0, name.length - 1)+","+val+"}");
			} else if (lc == 'v') {
				// It is value parameter
				var title = titles[name.substring(0, name.length - 1)];
				var type = types[name.substring(0, name.length - 1)];

				//alert("dfv{"+name.substring(0, name.length - 1)+","+val+","+title+","+type+"}");

				if (type == "p") {
					setPeopleFromFieldName(title, val);
				} else if (type == "t") {
					setTextFromFieldName(title, val);
				} else if (type == "l") {
					setLookupFromFieldName(title, val);
				} else if (type == "c") {
					setChoiceFromFieldName(title, val);
				} else if (type == "b") {
					setYesNoFromFieldName(title, val);
				} else {
					setLookupFromFieldName(title, val);
				}
			} else if (lc == 'h') {
				// It is hide parameter
				var title = titles[name.substring(0, name.length - 1)];
				var type = types[name.substring(0, name.length - 1)];

				//alert("dfh{"+name.substring(0, name.length - 1)+","+val+","+title+","+type+"}");

				if (type == "p") {
					setHidePeopleFromFieldName(title, val);
				} else if (type == "t") {
					setHideTextFromFieldName(title, val);
				} else if (type == "l") {
					setHideLookupFromFieldName(title, val);
				} else if (type == "c") {
					setHideChoiceFromFieldName(title, val);
				} else if (type == "b") {
					setHideYesNoFromFieldName(title, val);
				} else {
					setHideLookupFromFieldName(title, val);
				}
			} else if (lc == 'e') {
				var title = titles[name.substring(0, name.length - 1)];
				var type = types[name.substring(0, name.length - 1)];

				//alert("dfe{"+name.substring(0, name.length - 1)+","+val+","+title+","+type+"}");

				var control = document.getElementById(title);

				if (control == null) continue;

				if (type == "h") {
					if (val == "hide") control.style.display = "none";
					else control.style.display = "";
				} else if (type == "t") {
				} else {
				}
			}
		}
	}
}